package cn.iocoder.yudao.module.knowledge.service.elecdata;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.knowledge.controller.admin.elecdata.vo.*;
import cn.iocoder.yudao.module.knowledge.dal.dataobject.elecdata.ElecDataDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.knowledge.dal.mysql.elecdata.ElecDataMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.knowledge.enums.ErrorCodeConstants.*;

/**
 * 用电信息 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ElecDataServiceImpl implements ElecDataService {

    @Resource
    private ElecDataMapper elecDataMapper;

    @Override
    public Long createElecData(ElecDataSaveReqVO createReqVO) {
        // 插入
        ElecDataDO elecData = BeanUtils.toBean(createReqVO, ElecDataDO.class);
        elecDataMapper.insert(elecData);

        // 返回
        return elecData.getId();
    }

    @Override
    public void updateElecData(ElecDataSaveReqVO updateReqVO) {
        // 校验存在
        validateElecDataExists(updateReqVO.getId());
        // 更新
        ElecDataDO updateObj = BeanUtils.toBean(updateReqVO, ElecDataDO.class);
        elecDataMapper.updateById(updateObj);
    }

    @Override
    public void deleteElecData(Long id) {
        // 校验存在
        validateElecDataExists(id);
        // 删除
        elecDataMapper.deleteById(id);
    }

    @Override
        public void deleteElecDataListByIds(List<Long> ids) {
        // 删除
        elecDataMapper.deleteByIds(ids);
        }


    private void validateElecDataExists(Long id) {
        if (elecDataMapper.selectById(id) == null) {
            throw exception(ELEC_DATA_NOT_EXISTS);
        }
    }

    @Override
    public ElecDataDO getElecData(Long id) {
        return elecDataMapper.selectById(id);
    }

    @Override
    public PageResult<ElecDataDO> getElecDataPage(ElecDataPageReqVO pageReqVO) {
        return elecDataMapper.selectPage(pageReqVO);
    }

}